import OpenAI from 'openai';
import dotenv from 'dotenv';
import Koa from 'koa';
import cors from '@koa/cors';
import router from 'koa-route';

dotenv.config({ path: '.env' });

const client = new OpenAI({
      apiKey: process.env.OPENAI_KEY,
      baseURL: process.env.BASE_URL,
});

const genSaleReport = async (data, query) => {
      const prompt = `
    你是一个王者荣耀解说员，上面说出该英雄的名字，你就得对该英雄进行介绍，
   :\n
    ${data}\n\n

    Please generate a report to answer the following question:\n
    ${query}
  `;
      const response = await client.chat.completions.create({
            model: 'gpt-3.5-turbo',
            temperature: 0.0,
            messages: [{ role: 'user', content: prompt }],
      });
      return response.choices[0].message.content;
};

const app = new Koa();

const main = async (ctx) => {
      const { Name } = ctx.request.query; // 获取查询参数中的 Name
      const final = await genSaleReport('', `介绍一下王者荣耀里面的${Name}`); // 使用空字符串作为 data 参数
      ctx.body = final;
      console.log(Name);
};

app.use(cors());
app.use(router.get('/', main));

app.listen(3000, () => {
      console.log('listening on port 3000');
});